﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormApplication.FormBase;
using WindowsFormsView;
using WindowsFormsPresenter.District;
using WindowsFormsModel.BusinessObjects;

namespace WindowsFormApplication.FormCategory
{
    public partial class frmDistrict : frmBaseCategory, IDistrictsView, IDistrictView
    {
        private DistrictsPresenter _districtsPresenter;
        private DistrictPresenter _districtPresenter;

        private static int totalPage = 0;
        private static int currentPage = 0;

        public IList<DistrictModel> Districts
        {
            set
            {
                var districts = value;
                if ((districts != null) && (districts.Count > 0))
                {
                    grdMain.DataSource = districts;

                    for (int i = 0; i < grdMain.DisplayLayout.Bands[0].Columns.Count; i++) { grdMain.DisplayLayout.Bands[0].Columns[i].Hidden = true; }

                    grdMain.DisplayLayout.Bands[0].Columns["DistrictCode"].Hidden = false;
                    grdMain.DisplayLayout.Bands[0].Columns["DistrictName"].Hidden = false;
                    grdMain.DisplayLayout.Bands[0].Columns["Description"].Hidden = false;
                    grdMain.DisplayLayout.Bands[0].Columns["IsActive"].Hidden = false;

                    grdMain.DisplayLayout.Bands[0].Columns["DistrictCode"].Header.Caption = "Mã tỉnh, thành phố";
                    grdMain.DisplayLayout.Bands[0].Columns["DistrictName"].Header.Caption = "Tên tỉnh, thành phố";
                    grdMain.DisplayLayout.Bands[0].Columns["Description"].Header.Caption = "Diễn giải";
                    grdMain.DisplayLayout.Bands[0].Columns["IsActive"].Header.Caption = "Được sử dụng";
                }
            }
        }

        public string DistrictCode { get; set; }

        public string DistrictName { get; set; }

        public DateTime CreateDate { get; set; }

        public DateTime ModifiedDate { get; set; }

        public string Description { get; set; }

        public bool IsActive { get; set; }

        protected override void InitData()
        {
            try
            {
                totalPage = _districtsPresenter.GetTotalPage();
                _districtsPresenter.DisplayByPage(currentPage);
                toolbarsManager.Tools["lblPage"].SharedProps.Caption = "Số trang " + (currentPage + 1) + "/ " + (totalPage + 1);
            }
            catch (ApplicationException ex) { ShowMessageBox(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        protected override void NextPage()
        {
            if (currentPage < totalPage)
            {
                currentPage = currentPage + 1;
                InitData();
            }
        }

        protected override void PreviousPage()
        {
            if (currentPage > 0)
            {
                currentPage = currentPage - 1;
                InitData();
            }
        }

        protected override void DeleteRow()
        {
            try
            {
                if (grdMain.ActiveRow.Index >= 0)
                {
                    string districtCode = grdMain.ActiveRow.Cells[TablePrimaryKey].Value.ToString();
                    new DistrictPresenter(null).Delete(districtCode);
                    currentPage = 0;
                    ShowMessageBox("Xóa dữ liệu hoàn thành !", "Thông báo xóa dữ liệu !", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    ShowMessageBox("Bạn chưa chọn dòng để xóa !", "Thông báo xóa dữ liệu !", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (ApplicationException ex) { ShowMessageBox(ex.Message, "Thông báo lỗi hệ thống !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        public frmDistrict()
        {
            InitializeComponent();

            _districtsPresenter = new DistrictsPresenter(this);
            _districtPresenter = new DistrictPresenter(this);
        }

        private void frmDistrict_Load(object sender, EventArgs e)
        {
            InitData();
        }
    }
}
